package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.system.domain.SysHotel;
import com.ruoyi.system.domain.vo.SysHotelRoomResp;
import com.ruoyi.system.enums.HotelRoomTypEnum;
import com.ruoyi.system.service.ISysHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysHotelRoomMapper;
import com.ruoyi.system.domain.SysHotelRoom;
import com.ruoyi.system.service.ISysHotelRoomService;

/**
 * 房型Service业务层处理
 * 
 * @author dht
 * @date 2025-04-28
 */
@Service
public class SysHotelRoomServiceImpl implements ISysHotelRoomService 
{
    @Autowired
    private SysHotelRoomMapper sysHotelRoomMapper;

    /**
     * 查询房型
     * 
     * @param hotelRoomId 房型主键
     * @return 房型
     */
    @Override
    public SysHotelRoom selectSysHotelRoomByHotelRoomId(Long hotelRoomId)
    {
        return sysHotelRoomMapper.selectSysHotelRoomByHotelRoomId(hotelRoomId);
    }

    /**
     * 查询房型列表
     * 
     * @param sysHotelRoom 房型
     * @return 房型
     */
    @Override
    public List<SysHotelRoom> selectSysHotelRoomList(SysHotelRoom sysHotelRoom)
    {
        return sysHotelRoomMapper.selectSysHotelRoomList(sysHotelRoom);
    }

    /**
     * 新增房型
     * 
     * @param sysHotelRoom 房型
     * @return 结果
     */
    @Override
    public int insertSysHotelRoom(SysHotelRoom sysHotelRoom)
    {
        sysHotelRoom.setRemainNumber(sysHotelRoom.getNumber());
        return sysHotelRoomMapper.insertSysHotelRoom(sysHotelRoom);
    }

    /**
     * 修改房型
     * 
     * @param sysHotelRoom 房型
     * @return 结果
     */
    @Override
    public int updateSysHotelRoom(SysHotelRoom sysHotelRoom)
    {
        SysHotelRoom hotelRoom = selectSysHotelRoomByHotelRoomId(sysHotelRoom.getHotelRoomId());
        long num = sysHotelRoom.getNumber() - hotelRoom.getNumber();
        sysHotelRoom.setRemainNumber(sysHotelRoom.getRemainNumber() + num);
        return sysHotelRoomMapper.updateSysHotelRoom(sysHotelRoom);
    }

    /**
     * 批量删除房型
     * 
     * @param hotelRoomIds 需要删除的房型主键
     * @return 结果
     */
    @Override
    public int deleteSysHotelRoomByHotelRoomIds(Long[] hotelRoomIds)
    {
        return sysHotelRoomMapper.deleteSysHotelRoomByHotelRoomIds(hotelRoomIds);
    }

    /**
     * 删除房型信息
     * 
     * @param hotelRoomId 房型主键
     * @return 结果
     */
    @Override
    public int deleteSysHotelRoomByHotelRoomId(Long hotelRoomId)
    {
        return sysHotelRoomMapper.deleteSysHotelRoomByHotelRoomId(hotelRoomId);
    }

    @Override
    public List<SysHotelRoomResp> listByHotelId(Long hotelId) {
        SysHotel sysHotel = SpringUtil.getBean(ISysHotelService.class).selectSysHotelByHotelId(hotelId);
        if (Objects.isNull(sysHotel)) {
            throw new GlobalException("酒店不存在");
        }
        return sysHotelRoomMapper.listByHotelId(hotelId).stream().map(item -> {
            SysHotelRoomResp resp = BeanUtil.copyProperties(item, SysHotelRoomResp.class);
            resp.setName(HotelRoomTypEnum.getByKey(item.getType()));
            return resp;
        }).collect(Collectors.toList());
    }

    @Override
    public int editBatch(List<SysHotelRoom> sysHotelRooms) {
        int i = 0;
        for (SysHotelRoom sysHotelRoom : sysHotelRooms) {
            i += updateSysHotelRoom(sysHotelRoom);
        }
        return i;
    }

    @Override
    public SysHotelRoom selectSysHotelRoomByHotelIdAndType(Long hotelId, Long hotelRoomType) {
        return sysHotelRoomMapper.selectSysHotelRoomByHotelIdAndType(hotelId, hotelRoomType);
    }
}
